package com.fitness.dao;

import com.fitness.entity.Project;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 项目表数据访问对象，继承 BaseDAO，实现项目相关数据库操作
 */
public class ProjectDAO extends BaseDAO {

    /**
     * 插入项目信息
     * @param project 项目实体对象
     * @return 插入是否成功（true/false）
     */
    public boolean insertProject(Project project) {
        String sql = "INSERT INTO 项目表(项目名称, 每小时价格, 项目描述) VALUES (?, ?, ?)";
        int affectedRows = executeUpdate(sql,
                project.getProjectName(),
                project.getPricePerHour(),
                project.getDescription());
        return affectedRows > 0;
    }

    /**
     * 根据ID删除项目
     * @param projectId 项目ID
     * @return 删除是否成功（true/false）
     */
    public boolean deleteProject(int projectId) {
        String sql = "DELETE FROM 项目表 WHERE 项目ID = ?";
        int affectedRows = executeUpdate(sql, projectId);
        return affectedRows > 0;
    }

    /**
     * 查询所有项目信息
     * @return 项目列表
     */
    public List<Project> getProjectList() {
        List<Project> projectList = new ArrayList<>();
        String sql = "SELECT * FROM 项目表";
        ResultSet rs = executeQuery(sql);
        try {
            while (rs != null && rs.next()) {
                Project project = new Project();
                project.setProjectId(rs.getInt("项目ID"));
                project.setProjectName(rs.getString("项目名称"));
                project.setPricePerHour(rs.getDouble("每小时价格"));
                project.setDescription(rs.getString("项目描述"));
                projectList.add(project);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeResources(null, null, rs);
        }
        return projectList;
    }
    public Project getProjectById(int projectId) {
        String sql = "SELECT * FROM 项目表 WHERE 项目ID = ?";
        ResultSet rs = executeQuery(sql, projectId);
        try {
            if (rs != null && rs.next()) {
                Project project = new Project();
                project.setProjectId(rs.getInt("项目ID"));
                project.setProjectName(rs.getString("项目名称"));
                project.setPricePerHour(rs.getDouble("每小时价格"));
                project.setDescription(rs.getString("项目描述"));
                return project;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeResources(null, null, rs);
        }
        return null;
    }
}